package com.next.servlet;

import com.bean.BookPage;
import com.bean.BookType;
import com.dao.BookTypeDao;
import com.utils.PageUtil;
import com.utils.ResponseUtils;
import com.utils.StringUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName BookTypeServlet
 * @Desc 后台图书分类控制层
 * @Author xiaoding
 * @Date 2021/1/18 14:55
 * @Version 1.0
 */
@WebServlet(name = "NextBookTypeServlet",value = "/NextBookTypeServlet")
public class BookTypeServlet extends HttpServlet {
    private BookTypeDao bookTypeDao = new BookTypeDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能模块
        String str = request.getParameter("flag");

        //判断功能模块
        if (str.equals("select")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //查询所有图书类型的数量
            int count = bookTypeDao.bookTypeCount();
            //定义访问的Servlet
            String targetUrl = "NextBookTypeServlet";
            //定义参数
            String param = "flag=select";
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用获取数据的方法
            List<BookType> bookType = bookTypeDao.bookTypeSelect(new BookPage(page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("bookType",bookType);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/bookType.jsp").forward(request, response);
        }else if (str.equals("search")) {
            //获取图书名称
            String bName = request.getParameter("bName");
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //查询所有图书类型的数量
            int count = bookTypeDao.bookTypeSearchCount("%"+bName+"%");
            //定义访问的Servlet
            String targetUrl = "NextBookTypeServlet";
            //定义参数
            String param = "flag=search&bName="+bName;
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用获取数据的方法
            List<BookType> bookType = bookTypeDao.bookTypeSearch(new BookPage("%" + bName + "%",page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("bookType",bookType);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/bookType.jsp").forward(request, response);
        }else if (str.equals("add")) {
            //获取图书类型的名称
            String bNames = request.getParameter("bNames");
            //设置路径、状态
            String message = null;
            String url = null;

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //调用添加图书类型名称的方法
            int a = bookTypeDao.addBookType(new BookType(bNames));
            //判断是否添加成功
            if (a > 0) {
                message = "添加成功";
            }else {
                message = "添加失败";
            }

            //设置搜索名称
            String bName = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("bName"))) {
                bName = request.getParameter("bName");
                url = "NextBookTypeServlet?flag=search&page="+page+"&bName="+bName;
            }else {
                url = "NextBookTypeServlet?flag=select&page="+page;
            }

            //跳转路劲
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("update")) {
            //获取图书类型、图书类型Id
            int bId = Integer.parseInt(request.getParameter("bId"));
            String bNames = request.getParameter("bNames");

            //设置路径、状态
            String message = null;
            String url = null;

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //调用修改图书类型名称的方法
            int a = bookTypeDao.updateBookType(new BookType(bId,bNames));
            //判断是否添加成功
            if (a > 0) {
                message = "修改成功";
            }else {
                message = "修改失败";
            }

            //设置搜索名称
            String bName = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("bName"))) {
                bName = request.getParameter("bName");
                url = "NextBookTypeServlet?flag=search&page="+page+"&bName="+bName;
            }else {
                url = "NextBookTypeServlet?flag=select&page="+page;
            }

            //跳转路劲
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("delete")) {
            //获取图书类型Id
            int bId = Integer.parseInt(request.getParameter("bId"));

            //设置路径、状态
            String message = null;
            String url = null;

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //调用删除图书类型名称的方法
            int a = bookTypeDao.deleteBookType(bId);
            //判断是否添加成功
            if (a > 0) {
                message = "删除成功";
            }else {
                message = "删除失败";
            }

            //设置搜索名称
            String bName = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("bName"))) {
                bName = request.getParameter("bName");
                url = "NextBookTypeServlet?flag=search&page="+page+"&bName="+bName;
            }else {
                url = "NextBookTypeServlet?flag=select&page="+page;
            }

            //跳转路劲
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("selectJSON")) {
            //获取所有图书分类数据
            List<BookType> list = bookTypeDao.bookTypeSelect2();

            //将数据转换为JSON格式，并发送
            JSONObject obj = new JSONObject();
            //添加数据
            obj.put("data", JSONArray.fromObject(list));
            //发送数据
            ResponseUtils.writeHTML(response,obj.toString());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
